package com.videoaggre.zcl.ui.activity.agefans

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.kingja.loadsir.core.LoadService
import com.tencent.smtt.sdk.WebView
import com.videoaggre.zcl.R
import com.videoaggre.zcl.bean.AgefansDetails
import com.videoaggre.zcl.bean.AgefansDetailsTag
import com.videoaggre.zcl.common.*
import com.videoaggre.zcl.databinding.AgefansItemDescBinding
import com.videoaggre.zcl.helper.InjectBundle
import com.videoaggre.zcl.helper.OnTabSelectedListener
import com.videoaggre.zcl.helper.loadsir.LoadSirHelper
import com.videoaggre.zcl.parse.BaseParse
import com.videoaggre.zcl.ui.activity.BaseActivity
import com.videoaggre.zcl.ui.adapter.BaseRecyclerAdapter
import com.videoaggre.zcl.ui.adapter.BaseViewHolder
import com.videoaggre.zcl.ui.dialog.LoadingDialog
import com.videoaggre.zcl.ui.fragment.agefans.AgefansDetailInfoFragment
import com.videoaggre.zcl.ui.fragment.agefans.AgefansDetailIntrFragment
import com.videoaggre.zcl.ui.fragment.parseHtml
import com.videoaggre.zcl.ui.viewmodel.AgefansDetailsViewModel
import com.videoaggre.zcl.utils.FragmentChangeManager
import com.videoaggre.zcl.utils.GlideUtils
import com.videoaggre.zcl.utils.Logger
import com.videoaggre.zcl.utils.Utils
import kotlinx.android.synthetic.main.agefans_activity_details.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class AgefansDetailsActivity : BaseActivity(R.layout.agefans_activity_details) {
    companion object {
        private const val DETAILS_LINK = "mDetailsLink"
        fun open(context: Context, link: String) = context.startKtxActivity<AgefansDetailsActivity>(value = DETAILS_LINK to link)
    }

    private val mLoadService: LoadService<*> by lazy { LoadSirHelper.register(rootView) { onReload() } }
    private val mAgefansDetailsViewModel: AgefansDetailsViewModel by viewModels()
    private var mLoadingDialog: LoadingDialog? = null
    private var mFragmentChangeManager: FragmentChangeManager? = null
    private val mDetailsLink: String by InjectBundle()
    private lateinit var mAgefansDetails: AgefansDetails
    private val fragtments: MutableList<Fragment> = mutableListOf<Fragment>().apply {
        add(AgefansDetailIntrFragment())
        add(AgefansDetailInfoFragment())
    }

    override fun initView() {
        //initVideoView(videoView)
        mLoadingDialog = LoadingDialog(this)
        Utils.invasionStatusBar(this)
        tlFun.tabData { tab("简介");tab("详情") }
        tlFun.addOnTabSelectedListener(OnTabSelectedListener { mFragmentChangeManager?.setFragments(it.position) })
        mFragmentChangeManager = FragmentChangeManager(supportFragmentManager, R.id.frameLayout, fragtments)
    }

    override fun initData() {
        mAgefansDetailsViewModel.mAgefansDetailsLiveData.observe(this, ::agefansDetailsLoad)
        mAgefansDetailsViewModel.mVideoPlayerLiveData.observe(this, ::player)
        mLoadService.showLoadingCallback()
        onReload()
    }

    private fun onReload() {
        parseHtml(BaseParse(this, this, mDetailsLink, loadService = mLoadService) {
            mAgefansDetailsViewModel.parseDetails(it)
        })
    }

    private fun agefansDetailsLoad(agefansDetails: AgefansDetails) {
        mAgefansDetails = agefansDetails
        GlideUtils.intoView(this, mAgefansDetails.cover, albumImage)
        //mReceiverGroup.groupValue.putString(DataInter.Key.KEY_COVER_SHOW, mAgefansDetails.cover)
    }

    private fun player(url: String) {
        Logger.e("player==>${url}")
        webview.visibility = View.INVISIBLE
        webview.mIParse = object : BaseParse(null, this, url) {
            override fun start() {
                albumImage.visible()
                mLoadingDialog?.show()
            }

            override fun error() {
                mLoadingDialog?.dismiss()
                albumImage.gone()
            }

            override fun onPageFinished(webView: WebView, url: String) {
                lifecycleScope.launch {
                    webView.evaluateJavascript("""javascript:var age_playfram = $("#age_playfram");$("#root").remove("#age_playfram");$("#root").hide();$("body").append(age_playfram);$("body").css("background","#ffffff");Jsbridge.html(document.getElementsByTagName('html')[0].innerHTML);document.body.style.backgroundColor ='#000000'""") {}
                    delay(500)
                    webview.visible()
                    albumImage.gone()
                    mLoadingDialog?.dismiss()
                }
            }
        }
    }
}

class AgefansDetailsDescAdapter : BaseRecyclerAdapter<BaseViewHolder<AgefansItemDescBinding>, AgefansDetailsTag>() {
    override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(AgefansItemDescBinding.inflate(getLayoutInflate(parent.context), parent, false))
    override fun onBindViewHolder(holder: BaseViewHolder<AgefansItemDescBinding>, bean: AgefansDetailsTag, position: Int) {
        holder.mViewBinding.run {
            bean.run {
                tvDescName.text = tagName
                tvDescValue.text = tagValue
                setViewClick(position, root)
            }
        }
    }
}
